//
//  LGSportsEvaDetailTextCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/11.
//

#import "LGSportsEvaDetailTextCell.h"
#import "LGRecoverySectionView.h"

#define itemHeight  viewPix(26)
@interface LGSportsEvaDetailTextCell()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)LGRecoverySectionView *titleView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
@end

@implementation LGSportsEvaDetailTextCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.titleView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.height.equalTo(@(itemHeight*2));
        }];
    }
    return  self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleView.title = LGNSString(dataDic[@"group_name"]);
    
    for (UIView *view in self.itemArray) {
        [view removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    
    NSArray *questionArray = dataDic[@"questions"];
    CGFloat topMargin = kMaxY(self.titleView.frame);
    for (NSInteger i=0; i<questionArray.count; i++) {
        NSDictionary *questionDic = questionArray[i];
        if(LGNSString(questionDic[@"title"]).length>0){
            [self itemViewWithTop:topMargin title:LGNSString(questionDic[@"title"]) content:@"" name:@""];
            topMargin += itemHeight;
        }
        NSArray *itemArray = questionDic[@"items"];
        for (NSInteger j=0; j<itemArray.count; j++) {
            NSDictionary *itemDic = itemArray[j];
            [self itemViewWithTop:topMargin title:LGNSString(itemDic[@"title"]) content:LGNSString(itemDic[@"answer"]) name:LGNSString(itemDic[@"name"])];
            topMargin += itemHeight;
        }
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(topMargin));
    }];
}

//查看趋势图
-(void)itemButtonTouched:(UIButton *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkRecordChart:)]){
        [self.delegate checkRecordChart:sender.titleLabel.text];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(LGRecoverySectionView *)titleView{
    if(!_titleView){
        _titleView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(34))];
    }
    return _titleView;
}


-(void)itemViewWithTop:(CGFloat)topMargin title:(NSString *)title  content:(NSString *)content name:(NSString *)name{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, topMargin, Screen_W-viewPix(32), itemHeight)];
    UILabel *titleLabel = [UILabel lableWithText:title colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:titleLabel];
    
    UILabel *contentLabel = [UILabel lableWithText:content colorString:@"#333333" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:1];
    [baseView addSubview:contentLabel];

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView).offset(viewPix(16));
        make.centerY.equalTo(baseView);
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(baseView).offset(-viewPix(16));
        make.centerY.equalTo(baseView);
    }];
    
    if(name.length>0){
        UIButton *touchButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [touchButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [touchButton setTitle:name forState:UIControlStateNormal];
        [touchButton addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
        [baseView addSubview:touchButton];
        [touchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(baseView);
        }];
    }
    
    [self.baseView addSubview:baseView];
    [self.itemArray addObject:baseView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
